/* 登录 */
function login(usr, psw, callback) {
    axios.get(`http://localhost:3000/usrinfo?usr=${usr}&psw=${encodeURIComponent(b64_md5(psw))}`)
        .then(res => {
            // console.log(res.data.length);
            callback(res.data.length > 0 ? {
                usr: res.data[0].usr,
                token: res.data[0].token
            } : "err");
        })
}
/* 注册 */
function regist(usr, psw, callback) {
    // console.log(usr, psw);
    axios.get(`http://localhost:3000/usrinfo?usr=${usr}`)
        .then(res => {
            // console.log(res.data, res.data.length == 0);
            if (res.data.length == 0) {
                const id = String(1 + res.data.length).padStart(5, '0');
                axios.post("http://localhost:3000/usrinfo", {
                    uid: id,
                    usr: usr,
                    psw: b64_md5(psw),
                    token: b64_md5(usr + id),
                    cartData: {},
                    favorite: []
                }).then(res => {
                    callback(true);
                })
            } else {
                callback(false);
            }
        })
}
/* 获取 */
function getData(key, callback) {
    axios.get(`http://localhost:3000/usrinfo${key}`)
        .then(res => callback(res.data))
        .catch(err => console.log(err));
}
/* 修改 */
function updateData(key, data, callback) {
    axios.put(`http://localhost:3000/usrinfo${key}`, data)
        .then(res => callback())
        .catch(err => console.log(err));
}
/* 添加订单 */

export default {
    login,
    regist,
    getData,
    updateData
};
/* 获取购物车 */
// function usrGetCart(token,callback,err) {
//     if(isLogin()) {

//     } else {
//         err()
//     }
// }